Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rock Paper Scissor by deso Initial Commit #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

deso-odoo
Copy link

No description provided.

Comment on lines +86 to +88
const random = () => {
return Math.floor(Math.random() * 3);
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep the declaration before usage, for better understanding.

Comment on lines +102 to +132
// const object = {
// p1: 1,
// p2: 2,
// p3: 3,
// p4: {
// p5: 4,
// },
// };
// console.log(document.getElementsByTagName("p"));

// const prom = new Promise((res, rej) => {
// setTimeout(() => {
// rej("Working");
// }, 10000);
// });

// console.log(
// prom.then(() => console.log("done")).catch(() => console.log("rejected"))
// );

fetch("https://portal.tycoonstats.com/api/demo")
.then((response) => response.text())
.then((data) => console.log(data));

async function a() {
let response = await fetch("https://portal.tycoonstats.com/api/demo");
let data = await response.text();
console.log(data);
}

a();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Make separate commit for different work.
  2. Double check your code before force pushing.
  3. Donot push unused / commented out code , make habit of checking the code with "git diff" before updating the commit.

Comment on lines +14 to +80
const play = () => {
rockBtn.addEventListener("click", () => {
let computerChoice = random();
computerChoiceSpan.innerText = computersOptions[computerChoice];
humanChoiceIcon.innerHTML =
'<i class="fa-solid fa-hand-back-fist fa-5x"></i>';
humanChoiceSpan.innerText = "Rock";
if (computerChoice === 0) {
result.innerText = "Draw";
computerChoiceIcon.innerHTML =
'<i class="fa-solid fa-hand-back-fist fa-5x"></i>';
} else if (computerChoice === 1) {
result.innerText = "Computer Won!!!";
computerChoiceIcon.innerHTML =
'<i class="fa-solid fa-hand fa-5x"></i>';
computerScore.innerText++;
} else {
result.innerText = "You Won!!!";
computerChoiceIcon.innerHTML =
'<i class="fa-solid fa-hand-scissors fa-5x"></i>';
humanScore.innerText++;
}
});
paperBtn.addEventListener("click", () => {
let computerChoice = random();
computerChoiceSpan.innerText = computersOptions[computerChoice];
humanChoiceIcon.innerHTML = '<i class="fa-solid fa-hand fa-5x"></i>';
humanChoiceSpan.innerText = "Paper";
if (computerChoice === 0) {
result.innerText = "You Won!!!";
computerChoiceIcon.innerHTML =
'<i class="fa-solid fa-hand-back-fist fa-5x"></i>';
humanScore.innerText++;
} else if (computerChoice === 1) {
computerChoiceIcon.innerHTML =
'<i class="fa-solid fa-hand fa-5x"></i>';
result.innerText = "Draw";
} else {
result.innerText = "Computer Won!!!";
computerChoiceIcon.innerHTML =
'<i class="fa-solid fa-hand-scissors fa-5x"></i>';
computerScore.innerText++;
}
});
scissorBtn.addEventListener("click", () => {
let computerChoice = random();
computerChoiceSpan.innerText = computersOptions[computerChoice];
humanChoiceIcon.innerHTML =
'<i class="fa-solid fa-hand-scissors fa-5x"></i>';
humanChoiceSpan.innerText = "Scissor";
if (computerChoice === 0) {
result.innerText = "Computer Won!!!";
computerChoiceIcon.innerHTML =
'<i class="fa-solid fa-hand-back-fist fa-5x"></i>';

computerScore.innerText++;
} else if (computerChoice === 1) {
result.innerText = "You Won!!!";
computerChoiceIcon.innerHTML =
'<i class="fa-solid fa-hand fa-5x"></i>';
humanScore.innerText++;
} else {
result.innerText = "Draw";
computerChoiceIcon.innerHTML =
'<i class="fa-solid fa-hand-scissors fa-5x"></i>';
}
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this specific case we can add logic of querySelectorAll("botton"), and for each button click we can call a method that will update the scenario who win.
Re- write the code: using concept of:
querySelectorAll("botton"), use foreach, and onclick call one method that will update the state, also avoid using multiple if...else and repeating code, use ternary operator as user can win in three cases only
you_win = ( (you === "rock") && (opponent === "scissor") ||
(you === "paper") && (opponent === "rock") ||
(you === "scissor") && (opponent === "rock") ) // returns true for your win cases only.
so (you === opponent) ? "draw message" : you_win ? call a method for updating score and message for user : call method for updating score and message fro computer;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants